<?php 
class Paymethod extends CS_Controller
{
    public function _init()
    {
        $this->load->library('pagination');
        $this->load->model('pay_method_model', 'pay_method');
    }
    
    public function grid($pg = 1)
    {
        $num = ($pg-1)*20;
        $config['base_url']    = base_url('paymethod/grid');
        $config['total_rows']  = $this->pay_method->total();
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_list'] = $this->pagination->create_links();
        $data['page_list'] = $this->pay_method->page_list($num);
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $this->load->view('paymethod/grid', $data);
    }
    
    public function add()
    {
        $this->load->view('paymethod/add');
    }
    
    public function addPost()
    {
        $error = $this->validate();
        if (empty($_FILES['bank_img']['name'])) {
            $error[] = '图片添加不能为空';
        }
        if (!empty($error)) {
            $this->error('paymethod/add', '', $error);
        }

        $imageData = $this->dealWithImages('bank_img', '', 'bank_img');
        if ($imageData == false) {
            $this->error('paymethod/add', '', '图片上传失败！');
        }
        
        $this->db->trans_start();
        $resultId = $this->pay_method->insertPayMethod($this->input->post(), $imageData);
        $this->db->trans_complete();
        
        if ($resultId) {
            $this->success('paymethod/grid', '', '保存成功！');
        } else {
            $this->error('paymethod/add', '', '保存失败！');
        }
    }
    
    public function edit($id)
    {
        $result = $this->pay_method->findById($id);
        if($result->num_rows() <= 0) {
            $this->error('paymethod/grid', '', '数据不存在！');
        }
        $data['paymethod'] = $result->row();
        $this->load->view('paymethod/edit', $data);
    }
    
    public function editPost()
    {
        $pay_method_id = $this->input->post('method_id');
        $error = $this->validate();
        if (!empty($error)) {
            $this->error('paymethod/edit', $pay_method_id, $error);
        }
        $imageData = '';
        if (!empty($_FILES['bank_img']['name'])) {
            $imageData = $this->dealWithImages('bank_img', $this->input->post('oldfilename'), 'bank_img');
            if ($imageData == false) {
                $this->error('paymethod/edit', $pay_method_id, '图片上传失败！');
            }
        }
        $this->db->trans_start();
        $resultId = $this->pay_method->updatePayMethod($this->input->post(), $imageData);
        $this->db->trans_complete();
    
        if ($resultId) {
            $this->success('paymethod/grid', '', '保存成功！');
        } else {
            $this->error('paymethod/edit', $pay_method_id, '保存失败！');
        }
    }

    public function delete($id)
    {
        $is_delete = $this->pay_method->deleteByMethodId($id);
        if (!$is_delete) {
            $this->error('paymethod/grid', '', '删除失败！');
        }
        
        $this->deleteOldFileName($this->input->get('bank_img'), 'advert');
        
        $this->success('paymethod/grid', '', '删除成功！');
    }
    
    public function validate()
    {
        $error = array();
        if ($this->validateParam($this->input->post('method_name'))) {
            $error[] = '支付名称不能为空。';
        }
        if ($this->validateParam($this->input->post('bank_name'))) {
            $error[] = '银行名称不能为空。';
        }
        if ($this->validateParam($this->input->post('bank_id'))) {
            $error[] = '银行编码不能为空。';
        }
        if ($this->validateParam($this->input->post('bank_code'))) {
            $error[] = '银行简码不能为空。';
        }
        if ($this->validateParam($this->input->post('shortcut'))) {
            $error[] = '支付类型不能为空。';
        }
        if ($this->validateParam($this->input->post('sort'))) {
            $error[] = '排序不能为空。';
        }
        
        return $error;
    }
}